package com.meteor.user.controller;

import com.meteor.framework.common.base.ResponseData;
import com.meteor.user.entity.SysOrg;
import com.meteor.user.query.SysOrgQuery;
import com.meteor.user.query.SysRoleQuery;
import com.meteor.user.query.SysRoleUserQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import com.meteor.framework.operatelog.annotations.OperateLog;
import com.meteor.framework.operatelog.enums.OperateTypeEnum;
import com.meteor.user.convert.SysOrgConvert;
import com.meteor.user.service.SysOrgService;
import com.meteor.user.vo.SysOrgVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构管理
 *
 * @author lynn
 * @date 2024/1/03
 */
@RestController
@RequestMapping("sys/org")
@Tag(name = "机构管理")
@AllArgsConstructor
public class SysOrgController {
    private final SysOrgService sysOrgService;

    @GetMapping("list")
    @Operation(summary = "列表")
//    @PreAuthorize("hasAuthority('sys:org:list')")
    public ResponseData list(@ParameterObject @Valid SysOrgQuery query) {
        List<SysOrgVO> list = sysOrgService.getList(query);

        return ResponseData.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
//    @PreAuthorize("hasAuthority('sys:org:info')")
    public ResponseData get(@PathVariable("id") String id) {
        SysOrg entity = sysOrgService.getById(id);
        SysOrgVO vo = SysOrgConvert.INSTANCE.convert(entity);

        // 获取上级机构名称
        if (!ObjectUtils.isEmpty(entity.getPid())) {
            SysOrg parentEntity = sysOrgService.getById(entity.getPid());
            vo.setParentName(parentEntity.getName());
        }

        return ResponseData.success(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('sys:org:save')")
    public ResponseData save(@RequestBody @Valid SysOrgVO vo) {
        sysOrgService.save(vo);

        return ResponseData.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:org:update')")
    public ResponseData update(@RequestBody @Valid SysOrgVO vo) {
        sysOrgService.update(vo);

        return ResponseData.success("修改成功");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:org:delete')")
    public ResponseData delete(@PathVariable("id") String id) {
        sysOrgService.delete(id);

        return ResponseData.success("删除成功");
    }

    @GetMapping("allList")
    @Operation(summary = "列表")
    public ResponseData allList(@ParameterObject @Valid SysOrgQuery query) {
        List<SysOrgVO> list = sysOrgService.getAllList(query);

        return ResponseData.success(list);
    }


    @GetMapping("/orgMerchant/{merchantId}")
    @Operation(summary = "根据商户id查询机构")
    @PreAuthorize("hasAuthority('sys:org:list')")
    public ResponseData orgMerchant(@PathVariable("merchantId") String merchantId) {
        List<SysOrgVO> list = sysOrgService.orgMerchant(merchantId);

        return ResponseData.success(list);
    }

}